// MyListBox.cpp : implementation file
//

#include "stdafx.h"
#include "ansieditor.h"
#include "MyListBox.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyListBox

CMyListBox::CMyListBox()
{
}

CMyListBox::~CMyListBox()
{
}


BEGIN_MESSAGE_MAP(CMyListBox, CListBox)
	//{{AFX_MSG_MAP(CMyListBox)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyListBox message handlers

void CMyListBox::DrawItem(LPDRAWITEMSTRUCT lpDis) 
{
	// TODO: Add your code to draw the specified item
	CDC dc;
	dc.Attach(lpDis->hDC);
	CRect rect = lpDis->rcItem;
	UINT nIndex = lpDis->itemID;

	CBrush *pBrush = new CBrush(::GetSysColor((lpDis->itemState & 
		ODS_SELECTED) ? COLOR_HIGHLIGHT : COLOR_WINDOW));
	dc.FillRect(rect, pBrush);
	delete pBrush;
	
	if(lpDis->itemState & ODS_FOCUS)
		dc.DrawFocusRect(rect);

	if(nIndex != (UINT)-1)
	{
		if(lpDis->itemState & ODS_SELECTED)
			dc.SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT));
		else
			dc.SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
		dc.SetBkMode(TRANSPARENT);
		MyListItem *pItem = (MyListItem*)lpDis->itemData;
		CString ts;
		ts = pItem->Text;
		if(pItem->hIcon)
		{
			dc.DrawIcon(rect.left + 5, rect.top + (rect.Height() - 32) / 2, pItem->hIcon);
		}
		dc.TextOut(rect.left + 42, rect.top + 13, ts);
		dc.SetBkMode(OPAQUE);
	}
	dc.Detach();	
}

void CMyListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
{
	lpMeasureItemStruct->itemHeight = 38;
}

void CMyListBox::DeleteItem(LPDELETEITEMSTRUCT lpDis) 
{
	// TODO: Add your specialized code here and/or call the base class
	MyListItem *item = (MyListItem*)lpDis->itemData;
	delete item;
}

int CMyListBox::AddItem(HICON hIcon, CString Text)
{
	int nIndex = this->AddString(_T(""));

	MyListItem *item = new MyListItem;
	item->hIcon = hIcon;
	item->Text = Text;
	SetItemData(nIndex, (DWORD)item);

	return nIndex;
}

void CMyListBox::PreSubclassWindow() 
{
//	ModifyStyle(LBS_OWNERDRAWVARIABLE|LBS_SORT, LBS_OWNERDRAWFIXED);
//	cs.style &= ~(LBS_OWNERDRAWVARIABLE|LBS_SORT);
//	cs.style |= LBS_OWNERDRAWFIXED;
	
	CListBox::PreSubclassWindow();
}

void CMyListBox::GetText(int nIndex, CString &rString)
{
	MyListItem *item = (MyListItem*)GetItemData(nIndex);
	rString = item->Text;
}